unit Unit8; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Unit1,
  sqldb, db, DBGrids, Menus, Unit9, Unit10, LR_DBSet, LR_Class;

type

  { TForm8 }

  TForm8 = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    frDBDataSet1: TfrDBDataSet;
    frReport1: TfrReport;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    SQLQuery1: TSQLQuery;
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormShow;
    procedure MenuItem10Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure ordenar_grid();
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem6Click(Sender: TObject);
    procedure MenuItem8Click(Sender: TObject);
    procedure MenuItem9Click(Sender: TObject);
  private
    nombre : String;
  public
    { public declarations }
  end; 

var
  Form8: TForm8; 

implementation

{ TForm8 }

procedure TForm8.ordenar_grid();
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  SQLQuery1.SQL.Text := 'select * from familia_producto where fmp_nombre ilike ''%' + nombre
    + '%'' order by fmp_nombre';

  SQLQuery1.Open;

  DBGrid1.Columns[0].Title.Caption := 'Código';
  DBGrid1.Columns[1].Title.Caption := 'Nombre';
  DBGrid1.Columns[2].Title.Caption := 'Descripción';

  DBGrid1.Columns[0].Width := 80;
  DBGrid1.Columns[1].Width := 200;
  DBGrid1.Columns[2].Width := 300;
end;

procedure TForm8.FormShow;
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;

  nombre := '%';
  
  ordenar_grid;
end;

procedure TForm8.DBGrid1CellClick(Column: TColumn);
var
  codigo : string;
  
begin
  codigo := SQLQuery1.FieldByName('fmp_cdg').AsString;
  
  Form10.codigo := codigo;
  Form10.ShowModal;
  
  ordenar_grid;
end;

procedure TForm8.MenuItem10Click(Sender: TObject);
begin
  frReport1.LoadFromFile('listado_familia_producto.lrf');
  frReport1.ShowReport;
end;

procedure TForm8.MenuItem2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm8.MenuItem4Click(Sender: TObject);
begin
  Form9.ShowModal;
  FormShow;
end;

procedure TForm8.MenuItem5Click(Sender: TObject);
var
  codigo : String;
  QueryString : String;

begin
  codigo := InputBox('Baja de Familia de Productos', 'Ingrese el código de familia de productos a eliminar:', '');

  if codigo <> '' then
  begin
    QueryString := 'delete from familia_producto where fmp_cdg = ' + codigo;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    ShowMessage('BAJA EXITOSA');
  end;

  FormShow;
end;

procedure TForm8.MenuItem6Click(Sender: TObject);
begin
  ShowMessage('Click en la familia de productos a modificar...');
end;

procedure TForm8.MenuItem8Click(Sender: TObject);
begin
  nombre := InputBox('Filtro de Familia de Productos por Nombre',
    'Ingrese parte del nombre de la familia de productos:', '');

  if nombre <> '' then
    ordenar_grid;
end;

procedure TForm8.MenuItem9Click(Sender: TObject);
begin
  FormShow;
end;

initialization
  {$I unit8.lrs}

end.

